<template>
  <div class="register-container">
    <div class="register-box">
      <div class="logo"><h1>注册</h1></div>
      <el-form ref="registerForm" :model="registerForm" :rules="rules" class="register-form">
        <el-form-item prop="username">
          <el-input
              v-model="registerForm.username"
              prefix-icon="el-icon-user"
              placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
              v-model="registerForm.password"
              prefix-icon="el-icon-lock"
              type="password"
              placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input
              v-model="registerForm.confirmPassword"
              prefix-icon="el-icon-lock"
              type="password"
              placeholder="请确认密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="phone">
          <el-input
              v-model="registerForm.phone"
              prefix-icon="el-icon-phone"
              placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" round class="register-btn" @click="handleRegister">注册</el-button>
        </el-form-item>
        <el-form-item class="login-link">
          <el-link type="primary" @click="$router.push('/')">已有账号？去登录</el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from 'axios';
import { Message } from 'element-ui';

export default {
  name: 'Register',
  data() {
    return {
      registerForm: {
        username: '',
        password: '',
        confirmPassword: '',
        phone: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能少于 6 个字符', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: this.validateConfirmPassword, trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号格式', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    validateConfirmPassword(rule, value, callback) {
      if (value !== this.registerForm.password) {
        callback(new Error('两次输入的密码不一致'));
      } else {
        callback();
      }
    },
    async handleRegister() {
      try {
        this.$refs.registerForm.validate(async (valid) => {
          if (valid) {
            const res = await axios.post(
                'http://localhost:8080/api/register',
                {
                  username: this.registerForm.username,
                  password: this.registerForm.password,
                  phone: this.registerForm.phone,
                  createTime: new Date().toLocaleString(),
                  money: 0,
                  sex: 0
                },
                { headers: { 'Content-Type': 'application/json' } }
            );

            if (res.data.code === 200) {
              Message.success('注册成功，请登录');
              this.$router.push('/');
            } else {
              Message.error(res.data.msg || '注册失败');
            }
          }
        });
      } catch (error) {
        console.error('注册请求异常：', error);
        Message.error('网络错误，注册失败');
      }
    }
  }
};
</script>

<style scoped>
.register-container {
  width: 100%;
  height: 100vh;
  background: linear-gradient(to bottom right, #6a11cb, #2575fc);
  display: flex;
  justify-content: center;
  align-items: center;
}
.register-box {
  width: 350px;
  background-color: rgba(255, 255, 255, 0.9);
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 0 15px rgba(0, 0, 0, 0.2);
}
.logo {
  text-align: center;
  margin-bottom: 20px;
}
.register-form {
  width: 100%;
}
.register-btn {
  width: 100%;
  padding: 10px 0;
}
.login-link {
  text-align: center;
  margin-top: 15px;
}
</style>